<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree
          :data="list"
          default-expand-all
        ><template v-slot="{data}">
          <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width:100%">
            <el-col :span="20">
              <svg-icon icon-class="home" /><span>{{ data.name }}

              </span>
            </el-col>
            <el-col :span="4">
              <el-row type="flex" justify="end">
                <!-- 两个内容 -->
                <el-col>{{ data.manager }}</el-col>
                <!-- 下拉菜单 element -->
                <el-col>
                  <el-dropdown>
                    <span>
                      操作<i class="el-icon-arrow-down" />
                    </span>
                    <el-dropdown-menu slot="dropdown">
                      <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                      <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                      <el-dropdown-item v-if="data.children.length===0" @click.native="hDel(data.id) ">删除部门</el-dropdown-item>
                    </el-dropdown-menu>
                  </el-dropdown>
                </el-col>
              </el-row>
            </el-col>
          </el-row>
        </template>
        </el-tree>
      </el-card>
    </div>
    <el-dialog
      :title="isEdit?'修改':'添加'"
      :visible.sync="showDialog"
      width="45%"
      :close-on-press-escape="false"
      :close-on-click-modal="false"
    ><dept-dialog v-if="showDialog" :pid="id" :is-edit="isEdit" :origin-list="originList" @success="hSuccess" @close="hClose" /></el-dialog>
  </div>
</template>
<script>
import { delDepartment, getDepartments } from '@/api/departments'
import deptDialog from './deptDialog.vue'
import { tranListToTreeData } from '@/utils'
export default {
  components: {
    deptDialog
  },
  data() {
    return {
      list: [],
      showDialog: false,
      id: '',
      isEdit: false,
      originList: []
    }
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    async loadDepartments() {
      const { data: res } = await getDepartments()
      // console.log(res)
      res.depts.shift()
      this.originList = res.depts.map(item => ({ id: item.id, pid: item.pid, name: item.name, code: item.code }))
      this.list = tranListToTreeData(res.depts)
      console.log(this.originList)
    },
    hAdd(data) {
      this.showDialog = true
      this.id = data
      this.isEdit = false
    },
    hEdit(data) {
      this.showDialog = true
      this.id = data
      this.isEdit = true
    },
    hSuccess() {
      this.showDialog = false
      this.loadDepartments()
    },
    hDel(data) {
      this.$confirm('真的要删除吗', '提示', { type: 'warning' }).then(async() => {
        try {
          const res = await delDepartment(data)
          console.log(res)
          this.$message.success(res.message)
          this.loadDepartments()
        } catch (e) {
          this.$message.error(e.message)
        }
      }).catch(() => {})
    },
    hClose() {
      this.showDialog = false
    }
  }

}
</script>
<style scoped>

</style>

